<template>
  <div class="container">
    <van-nav-bar
      title="登录"
    />
    <div class="top">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="pwd"
          type="pwd"
          name="pwd"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Form } from "vant";
import { Field } from "vant";
import { Button } from "vant";
import { NavBar } from "vant";

Vue.use(NavBar);

Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
export default {
  name: "Login",
  data() {
    return {
      username: "",
      pwd: "",
    };
  },
  methods: {
    async onSubmit(values) {
      let { username, pwd } = values;
      try {
        await this.$store.dispatch("loginMessage", { username, pwd });
        //路由跳转
        this.$router.replace("/home");
      } catch (error) {
        alert(error);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  margin: 0 auto;
  .top{
    margin-top: 100px;
  }
}
</style>